<template>
  <div id="pro_shop">
    <el-card class="box-card" shadow="always">
      <!--标题开始-->
      <div slot="header" class="clearfix">
        <span>商铺信息</span>
      </div>
      <!--标题结束-->
      <div>
        <!--表单开始-->
        <el-form
          ref="form"
          label-position="right"
          label-width="80px"
          :model="form"
          :rules="rules"
        >
          <el-form-item label="标题" prop="title">
            <el-input v-model="form.title"></el-input>
          </el-form-item>
          <!--商铺logo图片开始-->
          <el-form-item label="商铺logo">
            <el-upload
              class="avatar-uploader"
              action="http://192.168.1.99:8080/upload/admin"
              :show-file-list="false"
              :on-success="handleAvatarSuccess"
              :before-upload="beforeAvatarUpload"
              v-model="form.logoUrl"
            >
              <img v-if="form.logoUrl" :src="form.logoUrl" class="avatar" />
              <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
          </el-form-item>
          <!--商铺logo结束-->

          <!--描述开始-->
          <el-form-item label="描述" prop="description">
            <el-input v-model="form.description"></el-input>
          </el-form-item>
          <!--描述结束-->

          <!--地址开始-->
          <el-form-item label="地址" prop="address">
            <el-input v-model="form.address"></el-input>
          </el-form-item>
          <!--地址结束-->
        </el-form>
        <!--表单结束-->
      </div>
    </el-card>
    <!--修改按钮开始-->
    <div><el-button type="primary" @click="compile" v-power="'promote:merchant:update'">保存更改</el-button></div>
    <!--修改按钮结束-->
  </div>
</template>

<script>
import Shop from "@/api/promotion/Shop";
export default {
  data() {
    return {
      form: {
        title: "", //商铺标题
        logoUrl: "", //商铺logo
        description: "", //商铺描述
        address: "", //商铺地址
        showType: "" //商铺展示方式
      },
      imageUrl: "",
      rules: {
        //标题验证
        title: [{ message: "标题不能为空", trigger: "change", required: true }],
        //描述验证
        description: [
          { message: "描述验证不能为空", trigger: "change", required: true }
        ],
        //地址验证
        address: [
          { message: "地址不能为空", trigger: "change", required: true }
        ]
      }
    };
  },
  created() {
    this.gain(); //渲染页面
  },
  methods: {
    //渲染数据接口
    gain() {
      Shop.gain().then(res => {
        this.form = { ...res.data.data };
      });
    },
    //修改页面接口
    compile() {
      // this.$refs[form].validate(valid => {
      Shop.compile(this.form).then(res => {
        if (res.data.errmsg == "成功") {
          this.Prosperity.Messages("修改成功", "success");
        } else {
          this.Prosperity.Messages(res.data.errmsg, "error");
        }
      });
    },
    //上传图片
    handleAvatarSuccess(res) {
      console.log(res);
      this.form.logoUrl = res.url;
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === "image/jpeg";
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error("上传头像图片只能是 JPG 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!");
      }
      return isJPG && isLt2M;
    }
  }
};
</script>

<style scoped="scoped">
/*h头样式*/
.box-card {
  margin: 15px;
  margin-bottom: 0px;
}
.el-card__header {
  border-bottom: none !important;
}
.el-card__header span {
  font-weight: 600;
}

/*头像*/
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 143px;
  height: 143px;
  line-height: 143px;
  text-align: center;
  border: 1px dashed #d9d9d9;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
/*按钮*/
.el-button {
  margin-left: 15px;
}
</style>
